package com.ayuan.blog.service.impl;

import com.ayuan.blog.common.WeChatUtil;
import com.ayuan.blog.common.wechat.message.WeChatMessageClient;
import com.ayuan.blog.domain.Focus;
import com.ayuan.blog.domain.WxUser;
import com.ayuan.blog.service.FocusService;
import com.ayuan.blog.service.WxUserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.durcframework.core.expression.ExpressionQuery;
import org.durcframework.core.service.CrudService;
import com.ayuan.blog.service.WxMessageService;
import com.ayuan.blog.dao.WxMessageDao;
import com.ayuan.blog.domain.WxMessage;
import com.ayuan.blog.domain.sch.WxMessageSch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
    
@Service
@Log4j2
public class WxMessageServiceImpl 
        extends CrudService<WxMessage, WxMessageDao> 
        implements WxMessageService {

    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private FocusService focusService;

    @Override
    public PageInfo<WxMessage> findPage(WxMessageSch sch) {
        ExpressionQuery query = new ExpressionQuery();
        query.addPaginationInfo(sch);
        query.addAnnotionExpression(sch);

        long total = this.getDao().findTotalCount(query);
        List<WxMessage> list = this.getDao().find(query);

        PageInfo<WxMessage> page = new PageInfo<WxMessage>();
        page.setList(list);
        page.setPageNum(sch.getPageIndex()); // 设置当前页数
        page.setPageSize(sch.getPageSize()); // 设置每页的数量
        page.setSize(list.size()); // 设置当前页的数量
        page.setPages((int) ((total + sch.getPageSize() - 1) / sch.getPageSize())); // 设置总的页数
        page.setTotal(total); // 设置总的数量

        return page;
    }

    @Override
    public void dealWithWeChatMessage(String openId, Long userId, String content) {
        if (StringUtils.isEmpty(content)) {
            return;
        }
        String message = null;
        if (content.startsWith("?")) {

        } else if (content.startsWith("关注 ")) {
            message = focus(userId, content);
        } else if (content.startsWith("取消关注 ")) {

        } else if (content.startsWith("关注列表")) {
            message = getFocusList(userId);
        } else if (content.startsWith("发布状态 ")) {

        } else if(content.startsWith("状态列表")) {

        }
        if (StringUtils.isNotEmpty(message)) {
            WeChatMessageClient.messageCustomSend(WeChatUtil.getToken(), message, openId);
        }
    }

    private String focus(Long userId, String content) {
        String message = null;
        // 获取关注人
        String[] params = content.split(" ");
        if (params.length > 1) {
            log.info("nickname : " + params[1]);
            WxUser wxUser = wxUserService.getByUserName(params[1]);
            if (wxUser == null) {
                message = String.format("用户 %s 不存在", params[1]);
            } else {
                // 判断是否已经关注过
                Focus focus = focusService.getByUserIdAndFocusedUserId(userId, wxUser.getWxUserId());
                if (focus != null) {
                    // 已关注,提示已关注过
                    message = "已关注过该用户";
                } else {
                    // 未关注,关注
                    focus = new Focus();
                    focus.setUserId(userId);
                    focus.setFocusedUserId(wxUser.getWxUserId());
                    focusService.save(focus);
                    message = String.format("关注 %s 成功", params[1]);
                }
            }
        } else {
            // 提示命令错误
            message = "请输入正确的命令";
        }
        return message;
    }

    private String getFocusList(Long userId) {
        String message = "你关注的列表";

        List<Focus> focusList = focusService.getListByUserId(userId);
        for (int i = 0; i < focusList.size(); i++) {
            WxUser wxUser = wxUserService.get(focusList.get(i).getFocusedUserId());
            if (null != wxUser) {
                message += "\n" + wxUser.getNickname();
            }
        }

        return message;
    }
}